from pydantic import BaseModel
from typing import Optional, TypeVar

T = TypeVar('T')


class BaseResponse(BaseModel):
    code: int
    msg: str
    data: Optional[T]


class SuccessResponse(BaseResponse):
    code: int = 0
    msg: str = '成功'
    data: Optional[dict] = None


# backend
class Response101(BaseResponse):
    code: int = -101
    msg: str = '请求无效'
    data: Optional[dict] = None


class Response102(BaseResponse):
    code: int = -102
    msg: str = '参数无效'
    data: Optional[dict] = None


class Response103(BaseResponse):
    code: int = -103
    msg: str = '请求方法无效'
    data: Optional[dict] = None


class Response104(BaseResponse):
    code: int = -104
    msg: str = '请求端点不存在'
    data: Optional[dict] = None


class Response105(BaseResponse):
    code: int = -105
    msg: str = '后端异常'
    data: Optional[dict] = None


class Response201(BaseResponse):
    code: int = -201
    msg: str = '账号未登录'
    data: Optional[dict] = None


class Response202(BaseResponse):
    code: int = -202
    msg: str = '权限不足'
    data: Optional[dict] = None


class Response203(BaseResponse):
    code: int = -203
    msg: str = '当前账号已被封禁'
    data: Optional[dict] = None


class Response204(BaseResponse):
    code: int = -204
    msg: str = '用户名或者密码错误'
    data: Optional[dict] = None


class Response205(BaseResponse):
    code: int = -205
    msg: str = '不是你的账号，无法修改'
    data: Optional[dict] = None


class Response206(BaseResponse):
    code: int = -206
    msg: str = '用户名已存在'
    data: Optional[dict] = None


class Response207(BaseResponse):
    code: int = -207
    msg: str = '登录已失效'
    data: Optional[dict] = None


class Response208(BaseResponse):
    code: int = -208
    msg: str = '新密码不能和旧密码一样'
    data: Optional[dict] = None


class Response209(BaseResponse):
    code: int = -209
    msg: str = '账号不存在'
    data: Optional[dict] = None


class Response210(BaseResponse):
    code: int = -210
    msg: str = '不能操作其他管理员'
    data: Optional[dict] = None


class Response211(BaseResponse):
    code: int = -211
    msg: str = '危险行为，不能封禁自己'
    data: Optional[dict] = None


class Response212(BaseResponse):
    code: int = -212
    msg: str = '危险行为，不能删除自己'
    data: Optional[dict] = None


class Response213(BaseResponse):
    code: int = -213
    msg: str = '危险行为，不能对自己降权'
    data: Optional[dict] = None


class Response214(BaseResponse):
    code: int = -214
    msg: str = '危险行为，不能添加超级管理员'
    data: Optional[dict] = None


class Response301(BaseResponse):
    code: int = -301
    msg: str = '应用数量已超额'
    data: Optional[dict] = None


class Response302(BaseResponse):
    code: int = -302
    msg: str = '应用的额外数据格式不正确'
    data: Optional[dict] = None


class Response303(BaseResponse):
    code: int = -303
    msg: str = '应用不存在'
    data: Optional[dict] = None


class Response304(BaseResponse):
    code: int = -304
    msg: str = '应用已被超管禁用'
    data: Optional[dict] = None


class Response401(BaseResponse):
    code: int = -401
    msg: str = ''
    data: Optional[dict] = None


class Response402(BaseResponse):
    code: int = -402
    msg: str = '卡种的时间无效'
    data: Optional[dict] = None


class Response403(BaseResponse):
    code: int = -403
    msg: str = '卡种不存在'
    data: Optional[dict] = None


class Response501(BaseResponse):
    code: int = -501
    msg: str = '卡密绑定设备上限至少 1 台'
    data: Optional[dict] = None


class Response502(BaseResponse):
    code: int = -502
    msg: str = '卡密的额外数据格式不正确'
    data: Optional[dict] = None


class Response503(BaseResponse):
    code: int = -503
    msg: str = '卡密不存在'
    data: Optional[dict] = None


class Response603(BaseResponse):
    code: int = -603
    msg: str = '设备不存在'
    data: Optional[dict] = None


class Response703(BaseResponse):
    code: int = -703
    msg: str = '试用记录不存在'
    data: Optional[dict] = None


class Response802(BaseResponse):
    code: int = -802
    msg: str = '已存在同名版本'
    data: Optional[dict] = None


class Response803(BaseResponse):
    code: int = -803
    msg: str = '应用版本不存在'
    data: Optional[dict] = None


class Response903(BaseResponse):
    code: int = -903
    msg: str = '防火墙规则不存在'
    data: Optional[dict] = None


class Response1003(BaseResponse):
    code: int = -1003
    msg: str = '设备日志不存在'
    data: Optional[dict] = None


class Response1102(BaseResponse):
    code: int = -1102
    msg: str = '不能删除其他管理员的公告'
    data: Optional[dict] = None


class Response1103(BaseResponse):
    code: int = -1103
    msg: str = '公告不存在'
    data: Optional[dict] = None


class FailureResponseError(Exception):
    def __init__(self, code: int, msg: Optional[str] = None, data: Optional[dict] = None):
        self.code = code
        self.msg = msg
        self.data = data

    def response(self):
        response_class = self._get_response_class_by_code(self.code)
        if response_class:
            return response_class(msg=self.msg, data=self.data) if self.msg else response_class(data=self.data)
        else:
            return BaseResponse(code=self.code, msg="未知错误", data=None)

    @staticmethod
    def _get_response_class_by_code(code: int) -> Optional[type[BaseResponse]]:
        for name, obj in globals().items():
            if isinstance(obj, type) and issubclass(obj, BaseResponse) and obj is not BaseResponse:
                find_code = dict(obj.model_fields)['code'].default
                if find_code == code:
                    return obj
        return None


# develop
class Response10001(BaseResponse):
    code: int = 10001
    msg: str = '参数错误'
    data: Optional[dict] = None


class Response10002(BaseResponse):
    code: int = 10002
    msg: str = '时间戳无效'
    data: Optional[dict] = None


class Response10003(BaseResponse):
    code: int = 10003
    msg: str = '签名无效'
    data: Optional[dict] = None


class Response10004(BaseResponse):
    code: int = 10004
    msg: str = '非客户端不可请求'
    data: Optional[dict] = None


class Response10005(BaseResponse):
    code: int = 10005
    msg: str = '请求方法不匹配'
    data: Optional[dict] = None


class Response10006(BaseResponse):
    code: int = 10005
    msg: str = '服务端异常'
    data: Optional[dict] = None


class Response10007(BaseResponse):
    code: int = 10007
    msg: str = '请求端点不存在'
    data: Optional[dict] = None


class Response10101(BaseResponse):
    code: int = 10101
    msg: str = '应用不存在'
    data: Optional[dict] = None


class Response10102(BaseResponse):
    code: int = 10102
    msg: str = '应用不匹配'
    data: Optional[dict] = None


class Response10103(BaseResponse):
    code: int = 10103
    msg: str = '应用已关闭'
    data: Optional[dict] = None


class Response10201(BaseResponse):
    code: int = 10201
    msg: str = '卡密不存在'
    data: Optional[dict] = None


class Response10202(BaseResponse):
    code: int = 10202
    msg: str = '卡密已冻结'
    data: Optional[dict] = None


class Response10203(BaseResponse):
    code: int = 10203
    msg: str = '卡密已过期'
    data: Optional[dict] = None


class Response10204(BaseResponse):
    code: int = 10204
    msg: str = '消耗的卡密已被使用或冻结'
    data: Optional[dict] = None


class Response10205(BaseResponse):
    code: int = 10205
    msg: str = '卡密超过设备上限'
    data: Optional[dict] = None


class Response10206(BaseResponse):
    code: int = 10206
    msg: str = '卡密不能在非限定时间段登录'
    data: Optional[dict] = None


class Response10207(BaseResponse):
    code: int = 10207
    msg: str = '卡密不允许被充值'
    data: Optional[dict] = None


class Response10208(BaseResponse):
    code: int = 10208
    msg: str = '卡密状态未知，操作失败'
    data: Optional[dict] = None


class Response10209(BaseResponse):
    code: int = 10209
    msg: str = '卡密未激活，不可操作'
    data: Optional[dict] = None


class Response10210(BaseResponse):
    code: int = 10210
    msg: str = '两张卡密的可绑定设备数量不一致'
    data: Optional[dict] = None


class Response10301(BaseResponse):
    code: int = 10301
    msg: str = '卡密不允许解绑'
    data: Optional[dict] = None


class Response10302(BaseResponse):
    code: int = 10302
    msg: str = '卡密冻结不可解绑'
    data: Optional[dict] = None


class Response10303(BaseResponse):
    code: int = 10303
    msg: str = '卡密过期不可解绑'
    data: Optional[dict] = None


class Response10304(BaseResponse):
    code: int = 10304
    msg: str = '非绑定设备不可操作'
    data: Optional[dict] = None


class Response10305(BaseResponse):
    code: int = 10305
    msg: str = '剩余时长不足以解绑'
    data: Optional[dict] = None


class Response10402(BaseResponse):
    code: int = 10402
    msg: str = '心跳不得低于 30 秒'
    data: Optional[dict] = None


class Response10501(BaseResponse):
    code: int = 10501
    msg: str = '本应用不允许试用'
    data: Optional[dict] = None


class Response10502(BaseResponse):
    code: int = 10502
    msg: str = '试用次数已满，请使用卡密'
    data: Optional[dict] = None


class Response10503(BaseResponse):
    code: int = 10503
    msg: str = '试用已到期，请使用卡密'
    data: Optional[dict] = None


class Response10504(BaseResponse):
    code: int = 10504
    msg: str = '试用记录不存在，请重新登录'
    data: Optional[dict] = None


class Response10601(BaseResponse):
    code: int = 10601
    msg: str = '设备未登录'
    data: Optional[dict] = None


class ResponseNegative1(BaseResponse):
    code: int = -1
    msg: str = '防火墙隔离中，拒绝请求'
    data: Optional[dict] = None


if __name__ == '__main__':
    try:
        raise FailureResponseError(code=-106)
    except FailureResponseError as e:
        print(e.response())
